/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 9/30/2010
 * Time: 4:23 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using popLib;
using System.IO;


namespace TaskNoteManager
{
	/// <summary>
	/// Description of TaskListGrid.
	/// </summary>
	public partial class TaskListGrid : UserControl
	{
		DataTable datasource = DataLib.initDataTable( "Tasklist [ Finish, Task, Deadline ] " );
		string xmlFile;
		
		public delegate void NoteChange();
		public event NoteChange NoteChangeEvent;
			
		public TaskListGrid( string xmlFile )
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.xmlFile = xmlFile;
			if( !File.Exists(xmlFile) ){
				datasource.Rows.Add( new object[]{ false, "Demo", DateTime.Now.ToString("dddd dd/MM/yyyy HH:mm") } );
				datasource.WriteXml( xmlFile );
			}
			
			LoadData();
			//this.Dgv_TaskList.RowsRemoved += new System.Windows.Forms.DataGridViewRowsRemovedEventHandler(this.Dgv_TaskListRowsRemoved);			
			
			Dgv_TaskList.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
			Dgv_TaskList.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
		}
		
		public void SaveData(){
			datasource.WriteXml( xmlFile );
		}
		
		public void LoadData(){
			datasource.Rows.Clear();
			datasource.ReadXml(xmlFile);
			Dgv_TaskList.DataSource = datasource;
		}
		
		public void Remove(){
			if( File.Exists(xmlFile) )
				File.Delete(xmlFile);
		}
		
		public bool ChangeName(string file){
			if( File.Exists(file) ){
				FormLib.alert("This task already added!!");
				return false;
			}
			File.Delete(xmlFile);
			xmlFile = file;
			datasource.WriteXml(file);
			return true;
		}
		
		void Dgv_TaskListRowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
		{
			if( NoteChangeEvent != null ) NoteChangeEvent();
		}
		
		void Dgv_TaskListCellClick(object sender, DataGridViewCellEventArgs e)
		{
			if( e.RowIndex >= 0 && e.ColumnIndex == 2 ){
				PickTimeForm form = new PickTimeForm( Dgv_TaskList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() );
				form.StartPosition = FormStartPosition.CenterParent;
				form.ShowDialog();
				
				if( form.Result != 0 ){
					if( form.Result == 1 )
						Dgv_TaskList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ((DateTime)form.Tag).ToString("dddd dd/MM/yyyy HH:mm");
					else Dgv_TaskList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = null;
					if( NoteChangeEvent != null ) NoteChangeEvent();
				}
			}
		}
		
		void Dgv_TaskListCellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if( editRow == e.RowIndex && 
			   editColumn == e.ColumnIndex && 
			   editValue != Dgv_TaskList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value && 
			   NoteChangeEvent != null ) 
				NoteChangeEvent();
		}
		
		int editRow = -1;
		int editColumn = -1;
		object editValue = null;
		void Dgv_TaskListCellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			editRow = e.RowIndex;
			editColumn = e.ColumnIndex;
			editValue = Dgv_TaskList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
		}
	}
}
